﻿using ConsoleApp7;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Realize;
using System.Threading.Tasks;

namespace WebApplication9.Controllers.ClientSet
{
    public class SSDController : Controller
    {
        private readonly ILogger<SSDController> _logger;
        private readonly IConfiguration configuration;

        public SSDController(ILogger<SSDController> logger, IConfiguration configuration)
        {
            this._logger = logger;
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            ViewBag.s = "SSDController.Index";
            return View();
        }
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Add(string item_name)
        {
            //ISSD ssd = new SSD();
            IGM gm = new GM();
            int i = await gm.Insert(item_name, configuration["SqlServer"]);
            if (i > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                Log_file_storage.LogDebug(_logger, "第42行");
            }
            return View();
        }
        public async Task<IActionResult> Index2()
        {
            IGM gm = new GM();
            List<Group_Menu> zhi = await gm.Server(configuration["SqlServer"]);
            return Ok(new
            {
                list = zhi
            });
        }
        public async Task<IActionResult> Delete(int id)
        {
            IGM gm = new GM();
            int i = await gm.Delete(id, configuration["SqlServer"]);
            if (i > 0)
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            return Ok(new
            {
                zhi = "onok"
            }); ;
        }
        public async Task<IActionResult> Update(int id)
        {
            HttpContext.Session.SetInt32("ID", id);
            //ISSD ssd = new SSD();
            IGM gm = new GM();
            List<Group_Menu> zhi = await gm.Server2(id,configuration["SqlServer"]);
            ViewBag.sd = zhi[0].group_name;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Update(string group_name)
        {
            int id = (int)HttpContext.Session.GetInt32("ID");
            //ISSD ssd = new SSD();
            IGM gm = new GM();
            int i = await gm.Update(id, group_name, configuration["SqlServer"]);
            if (i > 0)
            {
                return RedirectToAction("Index", "SSD");
            }
            return Content("失败");
        }
    }
}
